package com.design.util.moveBox.select;

import com.design.config.MoveBoxConfig;
import com.design.method.enums.moveBox.MapStaticEnum;
import com.design.method.moveBox.MapStatic;

public class PanelMap <T> {
    private final MapStatic<T>[] statics;
    private int key = 0;
    private int page = 0;
    private final int page_size;
    public PanelMap(T[] files){
        this.statics = new MapStatic[files == null ? 0 : files.length];
        for(int i = 0; i < files.length; i ++){
            this.statics[i] = new MapStatic<>(files[i], MapStaticEnum.SUCCESS);
        }
        this.page_size = files.length / MoveBoxConfig.PAGE_SIZE + 1;
    }
    public MapStatic<T> getMapStatic(){
        return this.statics[this.page * MoveBoxConfig.PAGE_SIZE + key];
    }
    public int getKey() {
        return key;
    }
    public void setStatic(MapStaticEnum type, int key){
        this.statics[key].setType(type);
    }
    public MapStatic<T>[] getMapStatics(){
        if(this.statics.length == 0){
            return null;
        }
        int sub = this.statics.length - this.page * MoveBoxConfig.PAGE_SIZE;
        int len = Math.min(sub, MoveBoxConfig.PAGE_SIZE);
        MapStatic<T>[] mapStatics = new MapStatic[len];
        System.arraycopy(this.statics,this.page * MoveBoxConfig.PAGE_SIZE,mapStatics,0,len);
        return mapStatics;
    }
    public void add(boolean bool){
        if(bool){
            this.add();
        }else{
            this.sub();
        }
    }
    private void add(){
        this.key ++;
        if(this.key >= MoveBoxConfig.PAGE_SIZE || this.page * MoveBoxConfig.PAGE_SIZE + key >= this.statics.length){
            this.key = 0;
            this.addPage();
        }
    }
    private void sub(){
        this.key --;
        if(this.key < 0){
            this.subPage();
        }
    }
    private void addPage(){
        this.page ++;
        if(this.page == this.page_size){
            this.key = 0;
            this.page = 0;
        }
    }
    private void subPage(){
        this.page --;
        if(this.page < 0){
            this.page = this.page_size - 1;
        }
        this.key = Math.min(this.statics.length - this.page * MoveBoxConfig.PAGE_SIZE - 1 , MoveBoxConfig.PAGE_SIZE - 1);
    }
}
